package com.his.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.pojo.Register;
import com.his.service.RegisterService;
import com.his.vo.NumberVo;
import com.his.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*处理挂号相关请求*/
@RestController
public class RegisterController {

    //注入service
    @Autowired
    private RegisterService registerSerivce;

    /*处理挂号产生病历号的请求*/
    @CrossOrigin //处理跨域请求
    @GetMapping("/doCaseNumber")
    public NumberVo doCaseNumber(){
        return registerSerivce.initRegisterService();
    }

    /*处理挂号信息保存的请求*/
    @CrossOrigin
    @PostMapping("/saveRegister")
    public Map<String,Object> saveRegister(@RequestBody Register register){
        Map<String,Object> map=new HashMap<>();
        System.out.println("==="+register);
        try{
            register.setVisitState(1);
            registerSerivce.save(register);
            map.put("msg","挂号成功....");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","挂号失败.....");
            map.put("code",500);
        }

        return map;
    }

    /*处理医生当前接诊病人分页查询请求*/
    @CrossOrigin
    @GetMapping("/registerList")
    public Map<String,Object> registerList(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "2") Integer pageSize
            ,RegisterVo registerVo){
        registerVo.setEmpId(1); //指定当前登录医生的id
        return registerSerivce.queryRegisterListService(pageNum,pageSize,registerVo);

    }
    /*处理某个医生，当前接诊人数的请求*/
    @CrossOrigin
    @GetMapping("/countRegister")
    public Integer countRegister(){
        RegisterVo registerVo = new RegisterVo();
        registerVo.setEmpId(1);
        registerVo.setVisitState(2);
        return registerSerivce.queryRegisterCountService(registerVo);
    }

    /*处理加载某个病人病历信息请求
    * id 是通过路径变量传入，restful风格的参数
    * 需要传递的数据即是路径，也是后台需要参数
    * */
    @CrossOrigin
    @GetMapping("/queryRegiserInfo/{id}")
    public Register queryRegisterInfo(@PathVariable Integer id){
        return registerSerivce.getById(id);
    }

    /*处理修改病历状态的请求*/
    @CrossOrigin
    @GetMapping("/updateVisitState")
    public Map<String,Object> updateVisitState(Integer id
            ,Integer state){
        Map<String,Object> map=new HashMap<>();

        try{
            Register register=new Register();
            register.setId(id);
            register.setVisitState(state);
            registerSerivce.updateById(register);
            map.put("msg","创建病历成功....");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","创建病历失败.....");
            map.put("code",500);
        }
        return map;
    }

    /*处理根据病历号或者姓名查询或者信息*/
    @CrossOrigin //处理跨域请求
    @GetMapping("/queryRegister/{caseNumber}/{realName}")
    public Register queryRegister(@PathVariable String caseNumber
            ,@PathVariable String realName){
        QueryWrapper<Register> wrapper=new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(caseNumber),"case_number",caseNumber);
        wrapper.eq(!StringUtils.isEmpty(realName),"real_name",realName);
        return registerSerivce.getOne(wrapper);
    }

    /*处理查看某个医生已接诊患者列表*/
    @CrossOrigin //处理跨域请求
    @PostMapping("/queryVisitedList")
    public Map<String,Object> queryVisitedList(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "2") Integer pageSize,
            @RequestBody RegisterVo registerVo){
        registerVo.setEmpId(1);
        registerVo.setVisitState(2);
        return registerSerivce.queryVisitedListService(pageNum,pageSize,registerVo);

    }

    /*处理查看某个医生已接诊患者列表*/
    @CrossOrigin //处理跨域请求
    @PostMapping("/queryDrugRegList")
    public Map<String,Object> queryDrugRegList(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "2") Integer pageSize,
            @RequestBody RegisterVo registerVo){
        registerVo.setVisitState(3);
        return registerSerivce.queryDrugRegListService(pageNum,pageSize,registerVo);

    }

    /*处理医生当前接诊病人分页查询请求*/
    @CrossOrigin
    @GetMapping("/backRegisterList")
    public Map<String,Object> backRegisterList(
            @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "2") Integer pageSize
            ,RegisterVo registerVo){
        return registerSerivce.queryBackRegisterListService(pageNum,pageSize,registerVo);

    }
    /*处理退号请求*/
    @CrossOrigin
    @RequestMapping("/backRegister")
    public Map<String,Object> backRegister(Integer id,Integer state){
        Map<String,Object> map=new HashMap<>();

        try{
            Register register=new Register();
            register.setId(id);
            register.setVisitState(state);
            registerSerivce.updateById(register);
            map.put("msg","退号成功....");
            map.put("code",200);
        }catch (Exception ex){
            ex.printStackTrace();
            map.put("msg","退号失败.....");
            map.put("code",500);
        }
        return map;
    }

    /*处理查询或者费用明细请求*/
    @CrossOrigin
    @GetMapping("/queryPayList")
    public Map<String,Object> queryPayList(
             @RequestParam(defaultValue = "1") Integer pageNum
            ,@RequestParam(defaultValue = "3") Integer pageSize
            ,RegisterVo registerVo){
        return registerSerivce.queryPayRecordListService(pageNum,pageSize,registerVo);

    }

    @CrossOrigin //处理跨域请求
    @GetMapping("/queryRegisterPay")
    public Register queryRegisterPay(String caseNumber
            , String realName){
        QueryWrapper<Register> wrapper=new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(caseNumber),"case_number",caseNumber);
        wrapper.eq(!StringUtils.isEmpty(realName),"real_name",realName);
        return registerSerivce.getOne(wrapper);
    }

    /*查询退药患者列表，看诊状态为3的患者*/
    @CrossOrigin //处理跨域请求
    @PostMapping("/queryFinishReg")
    public List<Register> queryFinishReg(@RequestBody RegisterVo registerVo){
        registerVo.setVisitState(3);
        QueryWrapper<Register> wrapper=new QueryWrapper<>();
        String caseNumber= registerVo.getCaseNumber();
        String realName= registerVo.getRealName();
        wrapper.eq(!StringUtils.isEmpty(caseNumber),"case_number",caseNumber);
        wrapper.eq(!StringUtils.isEmpty(realName),"real_name",realName);
        wrapper.eq("visit_state",registerVo.getVisitState());
        return registerSerivce.list(wrapper);
    }
}
